﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.Util;

namespace Moogle
{
    /// <summary>
    /// Database CRUD wrapper class. This class has no target consumer, but should used
    /// by any class needing to query the internal moogle SQLite database.
    /// </summary>
    class dbCommands
    {
        #region Local Memory
        
        /// <summary>
        /// Constant using to identify (filter) entries created in the
        /// Android Log by this class.
        /// </summary>
        private const string TAG = "dbCommands";
        /// <summary>
        /// DbHelper class instance.
        /// </summary>
        private DBHelper dbHelp;
        
        #endregion
        #region Class Constructor
        public dbCommands(Context context)
        {
            dbHelp = new DBHelper(context);
            dbHelp.OnCreate(dbHelp.WritableDatabase);
            Log.Debug(TAG, "dbCommands instance created.");
        }
        #endregion
        #region Medication CRUD

        /// <summary>
        /// Value function used to get the 
        /// contents of the PrescribedMedication
        /// table as a collection of Mediction Objects.
        /// </summary>
        /// <returns>IList collection instance of Medication objects</returns>
        public IList<Medication> GetAllPrescribedMedication()
        {
            Android.Database.ICursor PrescribedMedicationCursor = dbHelp.ReadableDatabase.Query("PrescribedMedication", null, null, null, null, null, null, null);
            IList<Medication> _listMedication = null;

            Log.Debug(TAG, "PrescribedMedicationCursor.Count = " + PrescribedMedicationCursor.Count.ToString());

            if (PrescribedMedicationCursor.Count > 0)
            {
                _listMedication = new List<Medication>();

                while (PrescribedMedicationCursor.MoveToNext())
                {
                    Medication _med = MapPrescribedMedication(PrescribedMedicationCursor);
                    _listMedication.Add(_med);
                } // end while
            } // end if
            return _listMedication;
        }

        /// <summary>
        /// Value function that can be used to test
        /// whether a given Medication Name already
        /// exists in the PrescribedMedications Table.
        /// </summary>
        /// <param name="pName">String containing the name of the Medication to Test for.</param>
        /// <returns></returns>
        public Boolean ContainsMedicationName(String pName)
        {
            IList<Medication> _Meds = GetAllPrescribedMedication();
            foreach (Medication _med in _Meds)
            {
                if (_med.Name.Trim().ToUpper() == pName.Trim().ToUpper())
                    return true;
            } // end foreach
            return false;
        }

        public void DeleteMedicationType(String pType)
        {
            string[] _vals = new string[1];
            _vals[0] = pType.ToString();

            dbHelp.WritableDatabase.Delete("PrescribedMedication", "Type=?", _vals);
        }
        /// <summary>
        /// Value function used to add records to the PrescribedMedication Table.
        /// </summary>
        /// <param name="pType">String defining the type of the Medication. The value should be equal to "Rx" or "OTC".</param>
        /// <param name="pName">String defining the name of the Medication.</param>
        public long AddMedication(String pType, String pName)
        {
            var _values = new ContentValues();
            _values.Put("Type", pType);
            _values.Put("Name", pName);

            return dbHelp.WritableDatabase.Insert("PrescribedMedication", null, _values);
        }
        /// <summary>
        /// Control function that can be used
        /// to delete an individual PrescribeMedication record
        /// based on a requested PrescribeMedicationID value. 
        /// </summary>
        /// <param name="pPopTimeID">Integer value defining the target PrescribeMedicationID value.</param>
        public void DeleteMedication(int pPrescribedMedicationID)
        {
            string[] _vals = new string[1];
            _vals[0] = pPrescribedMedicationID.ToString();

            dbHelp.WritableDatabase.Delete("PrescribedMedication", "PrescribedMedicationID=?", _vals);
        }
        /// <summary>
        /// PrescribedMedication/Medication record/class converter. This value function
        /// accepts a cursor (or row) and returns
        /// a PopTime class instance.
        /// </summary>
        /// <param name="cursor">Android.Database.ICursor class instance containing the contents of a single PopTime record.</param>
        /// <returns>PrescribedMedication class instance whose properties have been initialized based on the requested record</returns>
        private Medication MapPrescribedMedication(Android.Database.ICursor cursor)
        {
            Medication _Med = new Medication();
            _Med.MedicationID = cursor.GetInt(0);
            _Med.Type = cursor.GetString(1);
            _Med.Name = cursor.GetString(2);
            return (_Med);
        }
        #endregion
        #region PopTime CRUD
        /// <summary>
        /// Value function used to get the 
        /// contents of the PrescribedMedication
        /// table as a collection of Mediction Objects.
        /// </summary>
        /// <returns>IList collection instance of Medication objects</returns>
        public IList<PopTime> GetAllPopTimes()
        {

            Android.Database.ICursor _PopTimeCursor = dbHelp.ReadableDatabase.Query("PopTime", null, null, null, null, null, null, null);
            IList<PopTime> _listPopTime = null;

            Log.Debug(TAG, "PopTimeCursor.Count = " + _PopTimeCursor.Count.ToString());

            if (_PopTimeCursor.Count > 0)
            {
                _listPopTime = new List<PopTime>();

                while (_PopTimeCursor.MoveToNext())
                {
                    PopTime _popTime = MapPopTime(_PopTimeCursor);
                    _listPopTime.Add(_popTime);
                } // end while
            } // end if
            return _listPopTime;
        }
        /// <summary>
        /// Value function used to add records to the PopTime Table.
        /// </summary>
        /// <param name="pMedicationName">String value defining the medication name.</param>
        /// <param name="pHour">Integer value defining the hour.</param>
        /// <param name="pMinute">Integer value defining the minute.</param>
        /// <param name="pAnteMeridiem">Boolean flag defining if the time is before noon</param>
        /// <param name="pPostMeridiem">Boolean flag defining if the time is after noon</param>
        /// <returns>returns a long value corresponding to the PopTimeID of the new record</returns>
        public long AddPopTime(String pMedicationName, Int32 pHour, Int32 pMinute, Boolean pAnteMeridiem, Boolean pPostMeridiem)
        {
            var _values = new ContentValues();
            
            _values.Put("MedicationName", pMedicationName);
            _values.Put("Hour", pHour);
            _values.Put("Minute", pMinute);
            
            int _intAnteMeridiem = 0;
            if (pAnteMeridiem == true)
                _intAnteMeridiem = 1;

            _values.Put("AnteMeridiem", _intAnteMeridiem);

            int _intPostMeridiem = 0;
            if (pPostMeridiem == true)
                _intPostMeridiem = 1;
            
            _values.Put("PostMeridiem", _intPostMeridiem);

            return dbHelp.WritableDatabase.Insert("PopTime", null, _values);
        }
        /// <summary>
        /// Control function that can be used to
        /// delete all PopTimes based on a given
        /// Medication Name.
        /// </summary>
        /// <param name="pMedicationName">String containing the Medication Name of the target pop times</param>
        public void DeleteMedicationPopTimes(String pMedicationName)
        {
            string[] _vals = new string[1];
            _vals[0] = pMedicationName.ToString();

            dbHelp.WritableDatabase.Delete("PopTime", "MedicationName=?", _vals);
        }
        /// <summary>
        /// Control function that can be used
        /// to delete an individual PopTime record
        /// based on a requested PopTimeID value. 
        /// </summary>
        /// <param name="pPopTimeID">Integer value defining the target PopTimeID value.</param>
        public void DeletePopTime(int pPopTimeID)
        {
            string[] _vals = new string[1];
            _vals[0] = pPopTimeID.ToString();

            dbHelp.WritableDatabase.Delete("PopTime", "PopTimeID=?", _vals);
        }
        /// <summary>
        /// record class converter. This value function
        /// accepts a cursor (or row) and returns
        /// a PopTime class instance.
        /// </summary>
        /// <param name="cursor">Android.Database.ICursor class instance containing the contents of a single PopTime record.</param>
        /// <returns>PopTime class instance whose properties have been initialized based on the requested record</returns>
        private PopTime MapPopTime(Android.Database.ICursor cursor)
        {
             PopTime _Pop = new PopTime();
            _Pop.PopTimeID = cursor.GetInt(0);
            _Pop.MedicationName = cursor.GetString(1);
            _Pop.Hour = cursor.GetInt(2);
            _Pop.Minute = cursor.GetInt(3);
            if(cursor.GetInt(4) == 1)
                _Pop.AnteMeridiem = true;
            else
                _Pop.AnteMeridiem = false;
            if(cursor.GetInt(5) == 1)
                _Pop.PostMeridiem = true;
            else
                _Pop.PostMeridiem = false;

            return (_Pop);
        }
        #endregion
    }
}
